﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace UnionManager.Models
{
    public class Article : BaseModel, IArticle
    {
        public int SaveArticle(BAIVIET art, bool isnew=true)
        {
            if (isnew)
            {
                BAIVIET new_art = new BAIVIET();
                new_art.TieuDe = art.TieuDe;
                new_art.NoiDungHienThi = art.NoiDungHienThi;
                new_art.NoiDungTomTat = art.NoiDungHienThi.Substring(0, 100) + "...";
                new_art.TrangThai = 1;
                new_art.NgayTao = DateTime.Now;
                new_art.NgayChinhSua = DateTime.Now;
                new_art.SLXem = 0;
                new_art.SLBinhLuan = 0;
                new_art.SLQuanTam = 0;
                new_art.Id_ThuMuc = art.Id_ThuMuc;
                new_art.Id_TacGia = art.Id_TacGia;
                DB.AddToBAIVIETs(new_art);
                CommitChanges();
                return new_art.Id_BaiViet;
            }
            return 1;
        }


        public BAIVIET GetArticle(int id_art)
        {
            if(DB.BAIVIETs.Any(a => a.Id_BaiViet == id_art)){
                return DB.BAIVIETs.Single(a => a.Id_BaiViet == id_art);
            }
            return null;
        }



        public List<BAIVIET> GetNewArticle(int num)
        {
            if (num > DB.BAIVIETs.Count())
            {
                num = DB.BAIVIETs.Count();
            }
            return (from art in DB.BAIVIETs
                    select art).OrderByDescending(a => a.NgayTao).Take(num).ToList();
        }
    }
}